//-----------------------------------------------------------------------
// <copyright file="Group.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.Live.Messenger
{
    using System;
    using System.ComponentModel;
    using Microsoft.Live.Core;

    /// <summary>
    /// Represents a group of instant messaging addresses.
    /// </summary>
    /// <remarks>
    /// Groups provide the ability for users to organize their contacts by placing them into groups. Use <see cref="GroupCollection.Create"/> to create a new group.
    /// </remarks>
    public sealed class Group : INotifyPropertyChanged
    {
        private readonly MessengerEntity entity;
        private readonly User user;

        private ContactCollection contacts;

        internal Group(MessengerEntity entity, User user)
        {
            this.entity = entity;
            this.entity.AttachEvent("PropertyChanged", this.OnPropertyChanged);

            this.user = user;
        }

        internal MessengerEntity Entity
        {
            get { return this.entity; }
        }

        /// <summary>
        /// Gets or sets the name of the group.
        /// </summary>
        public string Name
        {
            get { return this.entity.GetProperty<string>("Name"); }
            set
            {
                if (this.Name != value)
                {
                    if (!this.user.Groups.IsNameValid(value))
                    {
                        throw new ArgumentOutOfRangeException();
                    }

                    this.entity.SetProperty<string>("Name", value);
                }
            }
        }

        /// <summary>
        /// Gets the <see cref="ContactCollection"/> that contains the contacts in the group.
        /// </summary>
        public ContactCollection Contacts
        {
            get {
                if (this.contacts == null) {
                    this.contacts = new ContactCollection(this.entity.GetProperty("Contacts"), this.user);
                }
                return this.contacts;
            }
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(object sender, MessengerEventArgs e)
        {
            this.OnPropertyChanged(
                new PropertyChangedEventArgs(
                    e.Entity.GetProperty<string>("PropertyName")));
        }

        /// <summary>
        /// Raises the <see cref="PropertyChanged"/> event.
        /// </summary>
        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, e);
            }
        }
    }
}
